﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;

namespace SecuritiesAnalysis.ToolkitWinForm
{
    public static class HistoryPriceOperator
    {
        const string sha = "SHA:";
        const string she = "SHE:";

        static char[] sprow = { '|' };
        static char[] spcol = { ';' };
        static T_HistoryPrice model;
        static List<string> RowsList;
        static StringBuilder sb;

        public static List<string> GetRows(string sContent)
        {
            string[] rows = sContent.Split(sprow);
            RowsList = rows.ToList();
            RowsList.RemoveAt(RowsList.Count - 1);
            return RowsList;
        }

        public static T_HistoryPrice CreateModel(int scode, string sRow)
        {
            string[] cols = sRow.Split(spcol);

            model = new T_HistoryPrice
            {
                StockCode = scode,
                Date = Convert.ToDateTime(cols[0]),
                OpeningPrice = Convert.ToDecimal(cols[1]),
                CeilingPrice = Convert.ToDecimal(cols[2]),
                FloorPrice = Convert.ToDecimal(cols[3]),
                ClosingPrice = Convert.ToDecimal(cols[4]),
                Volume = Convert.ToInt32(cols[5])
            };

            return model;
        }

        public static string GetHistorybyHTTP(string code,string from,string to)
        {
            sb = new StringBuilder("http://www.google.cn/finance/historical?q=");
            sb.Append(code);
            //sb.Append("&startdate=2008-03-20&enddate=2009-03-25&start=0&num=100");
            sb.Append("&startdate=");
            sb.Append(from);
            sb.Append("&enddate=");
            sb.Append(to);
            sb.Append("&start=0&num=100");

            WebRequest request = WebRequest.Create(sb.ToString());
            WebResponse response = request.GetResponse();
            Stream resStream = response.GetResponseStream();

            StreamReader sr = new StreamReader(resStream, System.Text.Encoding.Default);
            string result = sr.ReadToEnd();
            resStream.Close();
            sr.Close();

            return result;
        }

        public static string MatchMainDIV(string shtml)
        {
            MatchCollection mc = Regex.Matches(shtml, "<div id=prices>(.*?)</div>", RegexOptions.IgnoreCase | RegexOptions.Singleline);

            string sResult = mc[0].Value.Trim().Remove(0, 190);
            sResult = sResult.Replace("<tr>", string.Empty).Replace("<td class=firstcol>", string.Empty).Replace("<td class=lastcol>", string.Empty).Replace("<td>", string.Empty);
            sResult = sResult.Replace(@"</tr>", "|").Replace(@"</td>", ";").Replace(",", string.Empty);
            sResult = Regex.Replace(sResult, @"[\n\r]", string.Empty);

            return sResult;
        }

        /// <summary>
        /// 构建股票代码
        /// </summary>
        /// <example>输入39，输出SHE:000039</example>
        /// <param name="code"></param>
        /// <returns></returns>
        public static string BuildCodeString(int code)
        {
            if (code > 599999)
            {
                return sha + code.ToString("000000");
            }
            else
            {
                return she + code.ToString("000000");
            }
        }
    }
}
